package com.embhealth.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.embhealth.constant.MessageConstant;
import com.embhealth.entity.PageResult;
import com.embhealth.entity.QueryPageBean;
import com.embhealth.entity.Result;
import com.embhealth.pojo.Sport;
import com.embhealth.service.SportService;
import com.embhealth.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: chuanzhi_helath
 * @description: 运动管理
 * @author: xu jiangdong
 * @DateTime: 2020-08-21 15:54
 **/
@RestController
@RequestMapping("/sport")
public class SportController {

    @Reference
    private SportService sportService;

    /**
     * 上传运动管理数据
     *
     * @param excelFile
     * @return
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) {
        try {
            List<String[]> strings = POIUtils.readExcel(excelFile);
            List<Sport> sports = new ArrayList<>();
            for (String[] string : strings) {
                Sport sport = new Sport();
                sport.setId(Integer.parseInt(string[0]));
                sport.setSerialNumber(string[1]);
                sport.setSportName(string[2]);
                sport.setSportRisk(string[3]);
                sport.setSportKind(string[4]);
                sport.setSportTime(string[5]);
                sport.setSportFrequency(string[6]);
                sport.setType(string[7]);
                sport.setStrength(string[8]);
                sport.setMET(string[9]);
                sport.setKilostepEquivalent(string[10]);
                sport.setKilostepEquivalentTime(string[11]);
                sport.setMinAge(string[12]);
                sport.setMaxAge(string[13]);
                sport.setActionParts(string[14]);
                sport.setActionMethod(string[15]);
                sport.setIsEnable(string[16]);
                sport.setConsumeCalories(string[17]);
                sport.setConsumeEnergy(string[18]);
                sports.add(sport);
            }
            sportService.handleAdd(sports);
            return new Result(true, MessageConstant.IMPORT_SPORT_SUCCESS);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.IMPORT_SPORT_FAIL);
        }
    }

    /**
     * 分页查询
     *
     * @return
     */
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return sportService.findPage(queryPageBean);
    }

    /**
     * 添加单项数据
     *
     * @param sport
     * @return
     */
    @RequestMapping("/handleAdd")
    public Result handleAdd(@RequestBody Sport sport) {
        try {
            sportService.handleAdd(sport);
            return new Result(true, MessageConstant.ADD_SPORT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SPORT_FAIL);
        }
    }
}
